package com.ruoyi.system.service.impl;

import java.math.BigDecimal;
import java.util.List;

import com.ruoyi.system.domain.SysClass;
import com.ruoyi.system.domain.SysContract;
import com.ruoyi.system.domain.SysContractLabel;
import com.ruoyi.system.service.ISysContractLabelService;
import com.ruoyi.system.service.ISysContractService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.system.mapper.SysClassLabelMapper;
import com.ruoyi.system.domain.SysClassLabel;
import com.ruoyi.system.service.ISysClassLabelService;
import org.springframework.util.CollectionUtils;

/**
 * 排课明细Service业务层处理
 *
 * @author ruoyi
 * @date 2025-06-25
 */
@Service
public class SysClassLabelServiceImpl implements ISysClassLabelService
{
    @Autowired
    private SysClassLabelMapper sysClassLabelMapper;

    /** 合同明细service接口层 */
    @Autowired
    private ISysContractLabelService sysContractLabelService;

    /** 合同service接口层 */
    @Autowired
    private ISysContractService sysContractService;

    /**
     * 查询排课明细
     *
     * @param id 排课明细主键
     * @return 排课明细
     */
    @Override
    public SysClassLabel selectSysClassLabelById(Long id)
    {
        return sysClassLabelMapper.selectSysClassLabelById(id);
    }

    /**
     * 查询排课明细列表
     *
     * @param sysClassLabel 排课明细
     * @return 排课明细
     */
    @Override
    public List<SysClassLabel> selectSysClassLabelList(SysClassLabel sysClassLabel)
    {
        return sysClassLabelMapper.selectSysClassLabelList(sysClassLabel);
    }

    /**
     * 新增排课明细
     *
     * @param sysClassLabel 排课明细
     * @return 结果
     */
    @Override
    public int insertSysClassLabel(SysClassLabel sysClassLabel)
    {
        //查询合同明细
        SysContractLabel sysContractLabel = sysContractLabelService.selectSysContractLabelById(sysClassLabel.getContractLabelId());
        if (null == sysContractLabel) {
            throw new RuntimeException("未查询到合同明细信息，请联系管理员");
        }

        //合同明细剩余课时
        sysContractLabel.setRemainingClassTime(sysContractLabel.getRemainingClassTime() - 1);

        if (sysContractLabel.getRemainingClassTime() < 0){
            throw new RuntimeException("剩余课程已为零不可选择此明细");
        }

        //合同明细已排未上课时
        sysContractLabel.setNotDuringClassTime(sysContractLabel.getNotDuringClassTime() + 1);

        //修改合同明细
        sysContractLabelService.updateSysContractLabel(sysContractLabel);

        //查询合同
        SysContract sysContract = sysContractService.selectSysContractById(sysContractLabel.getContractId());
        if (sysContract.getIsStudying() == 0){
            sysContract.setIsStudying(1L);
            //修改合同方法
            sysContractService.updateSysContract(sysContract);
        }

        return sysClassLabelMapper.insertSysClassLabel(sysClassLabel);
    }

    /**
     * 修改排课明细
     *
     * @param sysClassLabel 排课明细
     * @return 结果
     */
    @Override
    public int updateSysClassLabel(SysClassLabel sysClassLabel)
    {
        return sysClassLabelMapper.updateSysClassLabel(sysClassLabel);
    }

    /**
     * 批量删除排课明细
     *
     * @param ids 需要删除的排课明细主键
     * @return 结果
     */
    @Override
    public int deleteSysClassLabelByIds(Long[] ids)
    {
        return sysClassLabelMapper.deleteSysClassLabelByIds(ids);
    }

    /**
     * 删除排课明细信息
     *
     * @param id 排课明细主键
     * @return 结果
     */
    @Override
    public int deleteSysClassLabelById(Long id)
    {
        return sysClassLabelMapper.deleteSysClassLabelById(id);
    }

    /**
     * 构建课程表明细
     * @param sysClass
     * @return
     */
    @Override
    public SysClassLabel buildSysClassLabel(SysClass sysClass) {
        SysClassLabel sysClassLabel = new SysClassLabel();
        //合同明细
        sysClassLabel.setContractLabelId(sysClass.getContractLabelId());
        return sysClassLabel;
    }

    /**
     * 根据排课删除排课明细
     * @param classId
     * @return
     */
    @Override
    public int deleteSysClassLabelByClassId(Long classId) {
        //根据排课查询排课明细
        List<SysClassLabel> sysClassLabelList = this.selectSysClassLabelByClassId(classId);
        if (!CollectionUtils.isEmpty(sysClassLabelList)){
            sysClassLabelList.forEach(it -> {
                //查询合同明细
                SysContractLabel sysContractLabel = sysContractLabelService.selectSysContractLabelById(it.getContractLabelId());
                if (null == sysContractLabel){
                    throw new RuntimeException("未查询到合同明细，请联系管理员");
                }
                //剩余课时
                sysContractLabel.setRemainingClassTime(sysContractLabel.getRemainingClassTime() + 1);
                //已排未上课时
                sysContractLabel.setNotDuringClassTime(sysContractLabel.getNotDuringClassTime() - 1);
                sysContractLabelService.updateSysContractLabel(sysContractLabel);
            });
        }
        return this.deleteSysClassLabelById(classId);
    }

    /**
     * 根据排课查询排课明细
     * @param classId
     * @return
     */
    @Override
    public List<SysClassLabel> selectSysClassLabelByClassId(Long classId) {
        return sysClassLabelMapper.selectSysClassLabelByClassId(classId);
    }

    @Override
    public List<SysClassLabel> selectSysClassLabels(Long classId) {
        return sysClassLabelMapper.selectSysClassLabels(classId);
    }
}